package org.geektimes.rest.demo;

import org.geektimes.rest.client.DefaultClient;
import org.geektimes.rest.client.DefaultClientBuilder;
import org.geektimes.rest.client.DefaultInvocationBuilder;
import org.geektimes.rest.client.ImmutableWebTarget;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.Response;
import java.util.HashMap;
import java.util.Map;

public class RestClientDemo {

    /**
     * 原始API进行处理
     */
    public static  void doOriginGet(){
        Client client = ClientBuilder.newClient();
        Response response = client
                .target("http://127.0.0.1:8080/hello/world")      // WebTarget
                .request() // Invocation.Builder
                .get();                                     //  Response
        String content = response.readEntity(String.class);
        System.out.println(content);
    }


    public static void main(String[] args) {
        doCustomPost();
    }

    /**
     * Post方法实现
     */
    public static  void doCustomPost(){
        DefaultClient defaultClient = (DefaultClient) new DefaultClientBuilder().build();
        ImmutableWebTarget target = (ImmutableWebTarget) defaultClient.target("http://127.0.0.1:7070/test2");
        DefaultInvocationBuilder invocationBuilder = (DefaultInvocationBuilder) target.request();
        invocationBuilder.header("Content-type","application/json");
        Map<String,String> params = new HashMap<String,String>();
        params.put("name","zhangsan");
        params.put("age","18");
        Entity entity = Entity.json(params);
        Response response = invocationBuilder.post(entity);
        String content = response.readEntity(String.class);
        System.out.println(content);
    }

}
